package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Recruit;
import cn.wolfcode.crm.domain.RecruitItem;
import cn.wolfcode.crm.mapper.RecruitItemMapper;
import cn.wolfcode.crm.mapper.RecruitMapper;
import cn.wolfcode.crm.query.RecruitQueryObject;
import cn.wolfcode.crm.service.IRecruitService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecruitServiceImpl implements IRecruitService {

    @Autowired
    private RecruitMapper mapper;
    @Autowired
    private RecruitItemMapper itemMapper;


    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void save(Recruit entity) {
        //设置申请人
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setStaff_Id(employee.getUsername());
        //设置审核状态
        entity.setExamineState(entity.STATE_NORMAL);
        mapper.insert(entity);
        //保存明细
        List<RecruitItem> recruitItems = entity.getRecruitItems();
        for (RecruitItem recruitItem : recruitItems) {
            recruitItem.setRecruitId(entity.getId());
            itemMapper.insert(recruitItem);
        }
    }

    public void update(Recruit entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public Recruit get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<?> list() {
        return mapper.selectAll();
    }

    public PageResult query(RecruitQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Recruit> list = mapper.queryForList(qo);
        return new PageResult(total,list);
    }

    public void audit(Long id) {
        //获取审核信息
            Recruit recruit = mapper.selectByPrimaryKey(id);
        //获取审核数据
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //设置审核人
        String username = employee.getUsername();
        mapper.audit(id,username);
    }
}
